package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.UserMedPlanEntity;

public class UserMedPlanDAO {
	public int insert(Connection conn, UserMedPlanEntity planEntity) throws SQLException {
		int id = -1;
		PreparedStatement ps = conn.prepareStatement(
				"insert into user_med_plan(period,period_unit,start_time,add_time,end_time,week_day,user_id,doc_id,status) values(?,?,?,?,?,?,?,?,?)",
				Statement.RETURN_GENERATED_KEYS);
		ps.setInt(1, planEntity.getPeriod());
		ps.setString(2, planEntity.getPeriod_unit());
		ps.setTimestamp(3, planEntity.getStart_time());
		ps.setTimestamp(4, planEntity.getAdd_time());
		ps.setTimestamp(5, planEntity.getEnd_time());
		ps.setString(6, planEntity.getWeek_day());
		ps.setInt(7, planEntity.getUser_id());
		ps.setInt(8, planEntity.getDoc_id());
		ps.setInt(9, planEntity.getStatus());
		if (ps.executeUpdate() > 0) {
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				id = rs.getInt(1);
			}
		}
		return id;
	}

	public boolean update(Connection conn, UserMedPlanEntity planEntity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update user_med_plan set period=?,period_unit=?,start_time=?,add_time=?,end_time=?,week_day=?,user_id=?,doc_id=?,status=? where id=?");
		ps.setInt(1, planEntity.getPeriod());
		ps.setString(2, planEntity.getPeriod_unit());
		ps.setTimestamp(3, planEntity.getStart_time());
		ps.setTimestamp(4, planEntity.getAdd_time());
		ps.setTimestamp(5, planEntity.getEnd_time());
		ps.setString(6, planEntity.getWeek_day());
		ps.setInt(7, planEntity.getUser_id());
		ps.setInt(8, planEntity.getDoc_id());
		ps.setInt(9, planEntity.getStatus());
		ps.setInt(10, planEntity.getId());
		if(ps.executeUpdate()>0) {
			b=true;
		}
		return b;
	}
	
	public List<UserMedPlanEntity> listByUserId(Connection conn ,int userId) throws SQLException{
		List<UserMedPlanEntity> list=new LinkedList<UserMedPlanEntity>();
		PreparedStatement ps=conn.prepareStatement("select * from user_med_plan where status=1 and user_id=? order by add_time desc");
		ps.setInt(1, userId);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			UserMedPlanEntity entity=fill(rs);
			list.add(entity);
		}
		return list;
	}
	
	public List<UserMedPlanEntity> listByDocId(Connection conn ,int doc_id) throws SQLException{
		List<UserMedPlanEntity> list=new LinkedList<UserMedPlanEntity>();
		PreparedStatement ps=conn.prepareStatement("select * from user_med_plan where status=1 and doc_id=? order by add_time desc");
		ps.setInt(1, doc_id);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			UserMedPlanEntity entity=fill(rs);
			list.add(entity);
		}
		return list;
	}

	private UserMedPlanEntity fill(ResultSet rs) throws SQLException {
		UserMedPlanEntity entity = new UserMedPlanEntity();
		entity.setId(rs.getInt("id"));
		entity.setAdd_time(rs.getTimestamp("add_time"));
		entity.setDoc_id(rs.getInt("doc_id"));
		entity.setEnd_time(rs.getTimestamp("end_time"));
		entity.setPeriod(rs.getInt("period"));
		entity.setPeriod_unit(rs.getString("period_unit"));
		entity.setStart_time(rs.getTimestamp("start_time"));
		entity.setStatus(rs.getInt("status"));
		entity.setUser_id(rs.getInt("user_id"));
		entity.setWeek_day(rs.getString("week_day"));
		return entity;
	}

	public UserMedPlanEntity findById(Connection conn, int id) throws SQLException {
		UserMedPlanEntity entity = null;
		PreparedStatement ps=conn.prepareStatement("select * from user_med_plan where status=1 and id=? order by add_time desc");
		ps.setInt(1, id);
		ResultSet rs=ps.executeQuery();
		if(rs.next()) {
			entity=fill(rs);
		}
		return entity;
	}

	public void deleteById(Connection conn, int id) throws SQLException {
		PreparedStatement ps=conn.prepareStatement("delete from user_med_plan where id = ?");
		ps.setInt(1, id);
		ps.executeUpdate();
	}
	
}
